package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.PageResult;
import com.heima.mapper.TaskMapper;
import com.heima.mapper.UserMapper;
import com.heima.pojo.Role;
import com.heima.pojo.User;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.dto.UserDto;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.pojo.vo.SearchUserWorkVo;
import com.heima.pojo.vo.TaskVo;
import com.heima.pojo.vo.UserVo;
import com.heima.service.IRoleService;
import com.heima.service.IUserService;
import com.heima.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public LoginUserVo login(LoginDto dto) {
        LoginUserVo vo = new LoginUserVo();
        if (dto == null) {
            //参数异常
            vo.setSuccess(false);
            vo.setMsg("用户名或密码不能为空");
            return vo;
        }

        User user = userMapper.selectOne(BeanUtil.toBean(dto, User.class));
        if (user == null) {
            //代表用户不存在得
            vo.setSuccess(false);
            vo.setMsg("用户不存在！");
            return vo;
        }
        if (!user.getPassword().equals(dto.getPassword())) {
            //代表密码错误
            vo.setSuccess(false);
            vo.setMsg("用户名或密码错误！");
            return vo;
        }
        //密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        UserThreadLocal.setUserId(Long.valueOf(user.getId()));
        loginUserVo.setToken(UUID.randomUUID().toString());
        return loginUserVo;
    }

    /**
     * 分页查询用户列表
     *
     * @param userName 用户名
     * @return
     */
    @Override
    public PageResult<UserVo> search(PageResult<UserVo> pageResult, String userName) {
        Page<UserVo> page = new Page<>(pageResult.getPageIndex(), pageResult.getPageSize());
        userMapper.search(page, userName);
        return pageResult.of(page, UserVo.class);
    }

    /**
     * 添加用户
     *
     * @param userDto 用户信息
     * @return
     */
    @Override
    public Boolean addUser(UserDto userDto) {
        User bean = BeanUtil.toBean(userDto, User.class);
        Role byId = roleService.getById(bean.getRoleId());
        bean.setRoleCode(byId.getRoleCode());
        bean.setRegionName(byId.getRoleName());
        return save(bean);
    }

    @Override
    public List<Map<String, Object>> repairerList(String innerCode) {
        List<Map<String, Object>> list = userMapper.repairerList(innerCode);
        return list.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> operatorList(String innerCode) {
        List<Map<String, Object>> list = userMapper.operatorList(innerCode);
        return list.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public PageResult<SearchUserWorkVo> searchUserWork(PageResult<SearchUserWorkVo> pageResult, String userName, Integer roleId, Boolean isRepair) {
        Page<SearchUserWorkVo> page = new Page<>(pageResult.getPageIndex(), pageResult.getPageSize());
        taskMapper.searchUserWork(page, userName, roleId, isRepair);
        return pageResult.of(page, SearchUserWorkVo.class);
    }

    @Override
    public UserVo getUser(Integer id) {
        return userMapper.getUser(id);
    }
}
